package net.pacman.view;

import net.pacman.R;
import net.pacman.model.CharacterBase;
import net.pacman.util.Constants;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceView;
import net.pacman.model.*;

public class CharacterView implements Renderer, Constants {

    private static final int PACMAN_VIEW_ID[] = {R.drawable.pacman0,
        R.drawable.pacman1, R.drawable.pacman2};
    private static final int DIE_VIEW_ID[] = {R.drawable.die0, R.drawable.die1,
        R.drawable.die2, R.drawable.die3, R.drawable.die4, R.drawable.die5,
        R.drawable.die6, R.drawable.die7, R.drawable.die8, R.drawable.die9,
        R.drawable.die10};
    private static final int CYAN_VIEW_ID[] = {R.drawable.cyan0,
        R.drawable.cyan1, R.drawable.cyan2, R.drawable.cyan3,
        R.drawable.cyan4, R.drawable.cyan5, R.drawable.cyan6, R.drawable.cyan7};
    private static final int PINK_VIEW_ID[] = {R.drawable.pink0,
        R.drawable.pink1, R.drawable.pink2, R.drawable.pink3,
        R.drawable.pink4, R.drawable.pink5, R.drawable.pink6, R.drawable.pink7};
    private static final int RED_VIEW_ID[] = {R.drawable.red0,
        R.drawable.red1, R.drawable.red2, R.drawable.red3,
        R.drawable.red4, R.drawable.red5, R.drawable.red6, R.drawable.red7};
    private static final int YELLOW_VIEW_ID[] = {R.drawable.yellow0,
        R.drawable.yellow1, R.drawable.yellow2, R.drawable.yellow3,
        R.drawable.yellow4, R.drawable.yellow5, R.drawable.yellow6, R.drawable.yellow7};
    private static final int FRIGHTENED_VIEW_ID[] = {R.drawable.frightened0,
        R.drawable.frightened1, R.drawable.frightened2, R.drawable.frightened3};
    
    private Bitmap[] bitmap;
    private Bitmap[] bitmap_another_stat;
    private int index;
    private int numbers_of_sprites;
    private int numbers_of_sprites_another_stat;
    private int spriteWidth;
    private int spriteHeight;
    private CharacterBase character;

    public CharacterView() {
        this.index = 0;
    }

    public CharacterView(SurfaceView view, Pacman character) {
        this();

        this.numbers_of_sprites = NUMBER_OF_IMAGE_PACMAN + 1;
        this.numbers_of_sprites_another_stat = NUMBER_OF_IMAGE_DIE;
        
        bitmap = new Bitmap[this.numbers_of_sprites];
        bitmap_another_stat = new Bitmap[NUMBER_OF_IMAGE_DIE];
        
        int i = 0;
        for (; i < this.numbers_of_sprites - 1; i++) {
            bitmap[i] = BitmapFactory.decodeResource(view.getResources(),
                    PACMAN_VIEW_ID[i]);
        }
        bitmap[i] = BitmapFactory.decodeResource(view.getResources(),
                PACMAN_VIEW_ID[1]);
        
        for (i = 0; i < this.numbers_of_sprites_another_stat; i++) {
            bitmap_another_stat[i] = BitmapFactory.decodeResource(view.getResources(),
                    DIE_VIEW_ID[i]);
        }
        
        this.character = character;
        this.spriteHeight = bitmap[0].getHeight();
        this.spriteWidth = bitmap[0].getWidth();

    }

    private void setGhostFrightened(SurfaceView view){
        for ( int i = 0; i < this.numbers_of_sprites_another_stat; i++) {
            bitmap_another_stat[i] = BitmapFactory.decodeResource(view.getResources(),
                    FRIGHTENED_VIEW_ID[i]);
        }
    }
    
    private CharacterView(Ghost character) {
        this();

        this.numbers_of_sprites = NUMBER_OF_IMAGE_GHOST;
        this.numbers_of_sprites_another_stat = NUMBER_OF_IMAGE_FRIGHTENED;
        
        bitmap = new Bitmap[this.numbers_of_sprites];
        bitmap_another_stat = new Bitmap[this.numbers_of_sprites_another_stat];
        
        
        this.character = character;
    }

    public CharacterView(SurfaceView view, SmartGhost ghost) {
        this(ghost);

        int i = 0;
        for (; i < this.numbers_of_sprites; i++) {
            bitmap[i] = BitmapFactory.decodeResource(view.getResources(),
                    CYAN_VIEW_ID[i]);
        }
        setGhostFrightened(view);
        
        this.spriteHeight = bitmap[0].getHeight();
        this.spriteWidth = bitmap[0].getWidth();
    }

    public CharacterView(SurfaceView view, DumbGhost ghost) {
        this(ghost);

        int i = 0;
        for (; i < this.numbers_of_sprites; i++) {
            bitmap[i] = BitmapFactory.decodeResource(view.getResources(),
                    YELLOW_VIEW_ID[i]);
        }
        setGhostFrightened(view);
        
        this.spriteHeight = bitmap[0].getHeight();
        this.spriteWidth = bitmap[0].getWidth();
    }

    public CharacterView(SurfaceView view, Ghost ghost, boolean isPink) {
        this(ghost);

        int i = 0;
        if (isPink) {
            for (; i < this.numbers_of_sprites; i++) {
                bitmap[i] = BitmapFactory.decodeResource(view.getResources(),
                        PINK_VIEW_ID[i]);
            }
        } else {
            for (; i < this.numbers_of_sprites; i++) {
                bitmap[i] = BitmapFactory.decodeResource(view.getResources(),
                        RED_VIEW_ID[i]);
            }
        }
        setGhostFrightened(view);
        
        this.spriteHeight = bitmap[0].getHeight();
        this.spriteWidth = bitmap[0].getWidth();
    }

    public void draw(Canvas canvas) {
        // TODO Auto-generated method stub
        if (character.isIsMoving()) {
            canvas.drawBitmap(bitmap[this.index], character.getX(), character.getY()
                    + this.spriteHeight, null);
            this.index = (this.index + 1) % this.numbers_of_sprites;
        } else {
            if (character.isExist()) {
                canvas.drawBitmap(bitmap[0], character.getX(), character.getY()
                        + this.spriteHeight, null);
                index = 0;
            }
        }
    }
}
